/**
 * 
 */
package com.mc.yingshi;

import java.io.IOException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 设备
 * @author w.weizhen
 *
 */
public class Device {
	private static String accessToken="at.7gu6567u7dn7vomp5eky9y1628nc8bk5-5j3ahmxfmk-0vpw104-emy59iet2";
    //private static String ="";//
	private static String deviceAdd="https://open.ys7.com/api/lapp/device/add";//添加设备到账号下
    private static String deviceList="https://open.ys7.com/api/lapp/device/list";//获取设备列表
    private static String deviceInfo="https://open.ys7.com/api/lapp/device/info";//获取单个设备信息
    private static String deviceCameraList="https://open.ys7.com/api/lapp/device/camera/list";//获取指定设备的通道信息
    private static String cameraNameUpdate="https://open.ys7.com/api/lapp/camera/name/update";//修改通道名称
    private static String deviceCapture="https://open.ys7.com/api/lapp/device/capture";//设备抓拍图片
    private static String addressGet ="https://open.ys7.com/api/lapp/v2/live/address/get";//获取播放地址
    private static String deviceCapacity ="https://open.ys7.com/api/lapp/device/capacity";//根据设备序列号查询设备能力集
    private static String deviceEncryptOff ="https://open.ys7.com/api/lapp/device/encrypt/off";//关闭加密
    /**
     * @param args
     */
    public static void main(String[] args) {
    	//add();//添加设备到账号下
    	//list();//获取设备列表
    	//info();//获取单个设备信息
    	//cameraList();//获取指定设备的通道信息
    	//updateCameraName();//修改通道名称
    	//capture();//设备抓拍图片
    	//getAddress();//获取播放地址
    	//capacity();//根据设备序列号查询设备能力集
    	encryptOff();//关闭加密
    	
    }
    
  //获取设备列表
    private static void add(){
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)
            .add("deviceSerial", "C65391420")
            .add("validateCode", "DMLHNQ")
            .build();
        Request request = new Request.Builder()
            .url(deviceAdd)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    //获取设备列表
    private static void list(){
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)
            .add("pageStart", "1")
            .add("pageSize", "50")
            .build();
        Request request = new Request.Builder()
            .url(deviceList)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
            
            JSONObject body = JSON.parseObject(responseBody);
            JSONArray datas = body.getJSONArray("data");
            
            for (int i = 0; i < datas.size(); i++) {
                //在线
                if ("1".equals(datas.getJSONObject(i).getString("status"))) {
                    System.out.println(datas.getJSONObject(i).getString("deviceSerial")+" 在线;");
                }
                //不在线
                if ("0".equals(datas.getJSONObject(i).getString("status"))) {
                    System.out.println(datas.getJSONObject(i).getString("deviceSerial")+" 不在线;");
                }
                //设备未上报
                if ("-1".equals(datas.getJSONObject(i).getString("status"))) {
                    System.out.println(datas.getJSONObject(i).getString("deviceSerial")+" 设备未上报;");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取单个设备信息
    private static void info(){
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)
            .add("deviceSerial", "F36615229")
            .build();
        Request request = new Request.Builder()
            .url(deviceInfo)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
            JSONObject body = JSON.parseObject(responseBody);
            
            String status="";
            //在线
            if ("1".equals(body.getJSONObject("data").getString("status"))) {status=" 在线;";}
            //不在线
            if ("0".equals(body.getJSONObject("data").getString("status"))) {status=" 不在线;";}
            //设备未上报
            if ("-1".equals(body.getJSONObject("data").getString("status"))) {status=" 设备未上报;";}
            System.out.println(body.getJSONObject("data").getString("deviceSerial")+status);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取指定设备的通道信息
    private static void cameraList()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)
            .add("deviceSerial", "F36615229")
            .build();
        Request request = new Request.Builder()
            .url(deviceCameraList)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String rsp = response.body().string();
            System.out.println(rsp);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //修改通道名称
    private static void updateCameraName()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)
            .add("deviceSerial", "F36615229")
            .add("channelNo", "1")
            .add("name", "通道名称，长度不大于50字节，不能包含特殊字符")
            .build();
        Request request = new Request.Builder()
            .url(cameraNameUpdate)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //设备抓拍图片 
    private static void capture()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)
            .add("deviceSerial", "F93882634")
            .add("channelNo", "1")
            .build();
        Request request = new Request.Builder()
            .url(deviceCapture)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //获取播放地址
    private static void getAddress()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "F36615229")//直播源，例如427734222，均采用英文符号，限制50个
            .add("channelNo", "1")//通道号,，非必选，默认为1
            .add("protocol", "1")//流播放协议，1-ezopen、2-hls、3-rtmp、4-flv，默认为1
            .add("quality", "1")//视频清晰度，1-高清（主码流）、2-流畅（子码流）
            .add("startTime", "2021-04-08 13:30:00")//ezopen协议地址的本地录像/云存储录像回放开始时间,示例：2019-12-01 00:00:00
            .add("stopTime", "2021-04-09 00:00:00")//ezopen协议地址的本地录像/云存储录像回放开始时间,示例：2019-12-01 00:00:00
            .add("type", "2")//ezopen协议地址的类型，1-预览，2-本地录像回放，3-云存储录像回放，非必选，默认为1
            .build();
        Request request = new Request.Builder()
            .url(addressGet)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //根据设备序列号查询设备能力集
    private static void capacity()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
            .add("accessToken", accessToken)//授权过程获取的access_token
            .add("deviceSerial", "F93882634")//直播源，例如427734222，均采用英文符号，限制50个
            .build();
        Request request = new Request.Builder()
            .url(deviceCapacity)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //关闭加密
    private static void encryptOff()
    {
    	OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
    		.add("accessToken", accessToken)
            .add("deviceSerial", "C65391420")
            .add("validateCode", "DMLHNQ")
            .build();
        Request request = new Request.Builder()
            .url(deviceCapacity)
            .post(requestBody)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String responseBody = response.body().string();
            System.out.println(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
}
